package edu.faytechcc.csc.messages;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import edu.faytechcc.csc.database.LogIntoDatabase;
import edu.faytechcc.csc.gui.ChatPanel;
import edu.faytechcc.csc.gui.ChatWindow;

/**
 * Please describe what this class does here.
 *
 * CSC 251 0001 DATE:
 *
 * @author Stephanie Mckenzie
 */
public class Connection
{

    private PrintWriter out;
    private BufferedReader in;
    private Socket socket;
    private String name;
    private String password;
    private String userName;
    private Scanner scan;
    private ChatWindow chat;
    private int index = 0;
    private boolean logged = false;

    /**
     *
     * @param socket
     * @param name
     */
    public Connection(Socket socket, String name)
    {
        this.socket = socket;
        this.name = name;
    }

    /**
     *
     * @throws IOException
     */
    public void connect() throws IOException
    {
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        (new RecieveMessagesThread()).start();
    }

    public void addChatWindow(ChatWindow chatWindow)
    {
        this.chat = chatWindow;
    }

    public void sendMessages(String message)
    {
        out.println(message);
    }

    public void logIn(String name, char[] password)
    {
        String pass = "";
        for(int index = 0; index < password.length; index++)
        {
            pass += password[index];
        }
        out.println("~~LogIn: ," + name + "," + pass);
    }
    
    private void recieveMessages()
    {
        try
        {
            String inputLine;
            while ((inputLine = in.readLine()) != null)
            {
                chat.displayMessage(name.toUpperCase(), inputLine);
                System.out.println(name.toUpperCase() + " SAID: " + inputLine);
                
                if (inputLine.equalsIgnoreCase(("CORRECT")))
                {
                    logged = true;
                }
                
                if (inputLine.equalsIgnoreCase(("BYE")))
                {
                    chat.displayMessage(name.toUpperCase(), "Connection closed by "
                            + name.toUpperCase() + " HIT ENTER TO EXIT");
                    break;
                }
            }
            out.close();
            in.close();
            if (!socket.isClosed())
            {
                socket.close();
            }
        } catch (IOException e)
        {
            System.out.println("Connection Closed.");
        }
    }

    /**
     * @return the logged
     */
    public boolean isLogged()
    {
        return logged;
    }
    
    /**
     *
     */
    private class RecieveMessagesThread extends Thread
    {

        @Override
        public void run()
        {
            recieveMessages();
        }
    }
}
